
import { subjectApi } from "@/api";
import { Col, Form, Input, Modal, Row, Spin, message } from "antd";
import React, { useState, useEffect, useImperativeHandle, } from "react";

const EditForm = ({ onRef, getList = Function }) => {
    const [show, setShow] = useState(false);
    const [loading, setLoading] = useState(false);
    const [form] = Form.useForm();

    /**@description 表单布局 */
    const createFormItemLayout = (xsSpan, smSpan) => ({
        labelCol: {
            xs: { span: xsSpan },
            sm: { span: 4 },
        },
        wrapperCol: {
            xs: { span: xsSpan },
            sm: { span: smSpan },
        },
    });
    const formItemLayout = createFormItemLayout(24, 20);

    /**@description 取消 */
    const onCanle = () => {
        setShow(false);
    }

    /**@description 添加|修改操作 */
    const open = async ({ type = "add", record = Object }) => {
        const { id = undefined } = record;
        setShow(true);
        form.resetFields();
        form.setFieldValue("type", "action." + type)




        if (type === "update") {
            if (id) {
                try {
                    setLoading(true);
                    const { data } = await subjectApi.getDetail(id);
                    form.setFieldsValue({
                        type: type || undefined,
                        ...data
                    });
                } catch (error) {
                    message.error(error)
                } finally {
                    setLoading(false);
                }
            }
        } else {
        }


    }

    /**@description 异步保存数据 */
    const saveDataToServer = async (object) => {

        try {
            const { data, code, msg } = await subjectApi.saveData(object);
            getList();
            onCanle();
        } catch (error) {

        } finally {

        }

    }

    /**@description 校验表单 */
    const validateForm = async () => {
        form.validateFields().then(res => {
            saveDataToServer(res)
        })
    }

    /**@description 向外暴露方法 */
    useImperativeHandle(onRef, () => ({
        open
    }))

    /**@description form表单props配置 */
    const formProps = {
        form,
        initData: {
            type: undefined,
            parentId: "0",
            menuType: "1"
        }
    }

    useEffect(() => {
        !show && form.resetFields()
    }, [show])



    return (
        <>
            <Modal
                title="专题编辑"
                width={550}
                open={show}
                centered={true}
                destroyOnClose={true}
                onCancel={onCanle}
                onOk={validateForm}
            >
                <Spin spinning={loading}>
                    <Form
                        {...formItemLayout}
                        {...formProps}
                    >
                        <Row gutter={24}>
                            <Col span={24} style={{ display: "none" }} >
                                <Form.Item label="id" name="id">
                                    <Input />
                                </Form.Item>
                            </Col>

                            <Col span={24}>
                                <Form.Item label="分类名称" name="title">
                                    <Input placeholder="请输入分类名称" />
                                </Form.Item>
                            </Col>
                            <Col span={24}>
                                <Form.Item label="备注" name="recommend">
                                    <Input.TextArea />
                                </Form.Item>
                            </Col>
                        </Row>
                    </Form>
                </Spin>

            </Modal>
        </>
    )
}
export default EditForm;